//
//  XLMineHeadImageTableViewCell.m
//  Mine
//
//  Created by 于鹏 on 2017/8/26.
//  Copyright © 2017年 YIVIEW. All rights reserved.
//

#import "XLMineHeadImageTableViewCell.h"
#import "XLMineHeadImageCellModel.h"
//#import <Masonry.h>
//#import <UIImageView+WebCache.h>
#import "Masonry.h"
#import "UIImageView+WebCache.h"

@interface XLMineHeadImageTableViewCell ()
//@property (nonatomic, strong)XLCellLabel *titleLabel;
@property (nonatomic, strong)XLCellLabel *detailLabel;
@property (nonatomic, strong)UIImageView *headImageView;

@end


@implementation XLMineHeadImageTableViewCell

#pragma mark - Override
+ (XLBaseTableViewCell *)cellWithIdentifier:(NSString *)cellIdentifier tableView:(UITableView *)tableView {
    XLMineHeadImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[XLMineHeadImageTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}

- (void)setupUI {
    [super setupUI];
    [self.contentView addSubview:self.headImageView];
//    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.detailLabel];
    
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.contentView).offset(XL_KCellMargin);
        make.bottom.equalTo(self.contentView).offset(-XL_KCellMargin);
        make.size.mas_equalTo(CGSizeMake(XL_KHeadImageWidth, XL_KHeadImageHeight));
    }];
    
    CGFloat titleMargin = 10;
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headImageView.mas_right).offset(XL_KCellMargin);
        make.bottom.equalTo(self.headImageView.mas_centerY).offset(-titleMargin/2);
    }];
    
    
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_left).offset(0);
        make.top.equalTo(self.headImageView.mas_centerY).offset(titleMargin/2);
    }];
    
     self.headImageView.layer.masksToBounds = YES;
}

- (void)setupDataModel:(XLBaseCellModel *)model {
    [super setupDataModel:model];
     XLMineHeadImageCellModel *headModel = (XLMineHeadImageCellModel *)model;
//    self.titleLabel.text = headModel.title;
    self.detailLabel.text = headModel.detailText;
    self.detailLabel.textColor = headModel.detailColor;
    self.detailLabel.font = headModel.detailFont;
    
    if (!headModel.imageIcon) {
        [self.headImageView sd_setImageWithURL:[NSURL URLWithString:headModel.imageUrl] placeholderImage:headModel.placeHolderImage];
    }else{
        self.headImageView.image = headModel.imageIcon;
    }
    self.headImageView.layer.cornerRadius = headModel.cornerRadius > 0 ? headModel.cornerRadius : 0.f;
    
    if (!CGSizeEqualToSize(headModel.imageSize, CGSizeZero)) {
        [self.headImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(headModel.imageSize);
        }];
    }
//    [self.headImageView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.size.mas_equalTo(headModel.imageSize);
//    }];

    
}

#pragma mark - Getter
//- (XLCellLabel *)titleLabel {
//    if (!_titleLabel) {
//        _titleLabel = [[XLCellLabel alloc]init];
//        _titleLabel.textColor = XL_KTitleColor;
//        _titleLabel.font = XL_KTitleFont;
//    }
//    return _titleLabel;
//}

- (XLCellLabel *)detailLabel {
    if (!_detailLabel) {
        _detailLabel = [[XLCellLabel alloc]init];
        _detailLabel.textColor = XL_KTitleColor;
        _detailLabel.font = XL_KTitleFont;
    }
    return _detailLabel;
}

- (UIImageView *)headImageView {
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc]init];
        _headImageView.contentMode = UIViewContentModeScaleAspectFill;
       
    }
    return _headImageView;
}
@end
